package com.example.project.controller.project;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.entity.Company;
import com.example.project.entity.Project;
import com.example.project.enums.BusinessType;
import com.example.project.service.CompanyService;
import com.example.project.service.ProjectService;
import com.example.project.utils.poi.ExcelUtil;

/**
 * 公司Controller
 * 
 * @author ruoyi
 * @date 2025-09-18
 */
@Controller
@RequestMapping("/project/company")
public class CompanyController extends BaseController {

	private String prefix = "pm/company";

	@Autowired
	private CompanyService companyService;

	@Autowired
	private ProjectService projectService;

	@RequiresPermissions("project:company:view")
	@GetMapping()
	public String company(Long projectId, ModelMap mmap) {
		mmap.put("projectId", projectId);
		mmap.put("projects", projectService.selectProjectById(projectId));
		mmap.put("projectList", projectService.findAllList(1));
		return prefix + "/company";
	}

	/**
	 * 查询公司列表
	 */
	@RequiresPermissions("project:company:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Company tCompany) {
		startPage();
		List<Company> list = companyService.selectCompanyList(tCompany);
		return getDataTable(list);
	}

	/**
	 * 导出公司列表
	 */
	@RequiresPermissions("project:company:export")
	@Log(title = "公司", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Company tCompany) {
		List<Company> list = companyService.selectCompanyList(tCompany);
		ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
		return util.exportExcel(list, "公司数据");
	}

	/**
	 * 新增公司
	 */
	@RequiresPermissions("project:company:add")
	@GetMapping("/add/{projectId}")
	public String add(@PathVariable("projectId") Long projectId, ModelMap mmap) {
		Project projects = projectService.selectProjectById(projectId);
		mmap.put("projects", projects);
		mmap.put("projectId", projectId);
		return prefix + "/add";
	}

	/**
	 * 新增保存公司
	 */
	@RequiresPermissions("project:company:add")
	@Log(title = "公司", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Company tCompany) {
		tCompany.setCreateBy(getLoginName());
		return toAjax(companyService.insertCompany(tCompany));
	}

	/**
	 * 修改公司
	 */
	@RequiresPermissions("project:company:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		Company tCompany = companyService.selectCompanyById(id);
		mmap.put("tCompany", tCompany);
		return prefix + "/edit";
	}

	/**
	 * 修改保存公司
	 */
	@RequiresPermissions("project:company:edit")
	@Log(title = "公司", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Company tCompany) {
		tCompany.setUpdateBy(getLoginName());
		return toAjax(companyService.updateCompany(tCompany));
	}

	/**
	 * 删除公司
	 */
	@RequiresPermissions("project:company:remove")
	@Log(title = "公司", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(companyService.deleteCompanyByIds(ids));
	}
}
